/**
 * @file voice_msg_test.c
 *
 */

/*********************
*      INCLUDES
*********************/
#include "lv_watch.h"

#if USE_LV_WATCH_VOICE_MSG != 0

#if VOICE_MSG_TEST != 0

/*********************
*      DEFINES
*********************/

/**********************
*      TYPEDEFS
**********************/

/**********************
*  STATIC PROTOTYPES
**********************/
static char * voice_msg_test_set_file_path(uint8_t index, uint8_t file_type);
static void voice_msg_test_write_file(void * data, uint32_t size, char * file_path);
static void voice_msg_adaptor_send_cnf(lv_task_t * task);
static bool voice_msg_test_is_group_msg(watch_app_adp_voice_msg_chat_type_t chat_type, char * name);

#if VOICE_MSG_APP_SPECIFIC_TEST != 0
static void voice_msg_test_fill_id(app_adaptor_voice_msg_t * msg);
static void voice_msg_test_rcv_msg_task_cb(lv_task_t * task);
#else
static void voice_msg_test_delete_info(app_adaptor_voice_msg_info_t * msg);
#endif

#ifdef BUILD_IN_PC_SIMULATOR /*for pc test*/
static void voice_msg_test_task_record_cb(lv_task_t * task);
static void voice_msg_test_task_voice_cb(lv_task_t * task);
#endif

/**********************
*  STATIC VARIABLES
**********************/
static char * test_name = NULL;
static lv_ll_t test_msgs_list; // app_adaptor_voice_msg_info_t
static uint8_t test_msgs_count;
static uint8_t test_voice_msg_count;

#if VOICE_MSG_APP_SPECIFIC_TEST != 0
static lv_task_t * voice_msg_test_rcv_msg_task;
#endif

#ifdef BUILD_IN_PC_SIMULATOR /*for pc test*/
static lv_task_t * voice_msg_test_task_record = NULL;
static uint8_t duration_test;
static uint8_t max_duration;
static AUDIO_RECORD_CB_FUNC func_record_test;
static CB_FUNC func_voice_test;
#endif

/**********************
*      MACROS
**********************/
#define VOICE_MSG_TEST_RCV_MSG_PERIOD    5000 /*simulate to receive the msg from server
                                               *with a period of 5 minutes*/
#define VOICE_MSG_TEST_RCV_MSG_NUM       3    /*the number of the msgs received from server*/

#define VOICE_MSG_TEST_FILE_DIR          "C:/"
#define VOICE_MSG_TEST_FILE_TEXT         "text.nv"
#define VOICE_MSG_TEST_FILE_VOICE        "voice.nv"
#define VOICE_MSG_TEST_AUDIO_POWER_ON    "C:/audio_power_on.mid"
#define VOICE_MSG_TEST_AUDIO_POWER_OFF   "C:/audio_power_off.mid"

/**********************
 *   GLOBAL FUNCTIONS
 ***********************/
void voice_msg_test_init(void)
{
    printf("%s\n", __FUNCTION__);
    /*write contacts info nvm for phonebook*/
    uint32_t length = sizeof(nv_watch_phonebook_t);
    nv_watch_phonebook_t * nvm_clear = (nv_watch_phonebook_t *)lv_mem_alloc(length);
    memset(nvm_clear, 0, length);
    nv_watch_phonebook_t nvm_list = {{
            {"admin", "1", 0, WATCH_PORTRAIT_ID_MOTHER},
            {"standby admin 1", "11", 1, WATCH_PORTRAIT_ID_FATHER},
            {"standby admin 2", "22", 1, WATCH_PORTRAIT_ID_GIRL},
            {"others 1", "111", 2, WATCH_PORTRAIT_ID_UNCLE},
            {"others 2", "222", 2, WATCH_PORTRAIT_ID_AUNT},
            {"others 3", "333", 2, WATCH_PORTRAIT_ID_PATERNAL_GRADFATHER},
            {"others 4", "444", 2, WATCH_PORTRAIT_ID_MATERNAL_GRADMOTHER},
            {"others 5", "555", 2, WATCH_PORTRAIT_ID_BOY},
            {"others 6", "666", 2, WATCH_PORTRAIT_ID_OTHERS},
        }
    };
    /*clear nvm*/
    if(length != UI_NV_Write_Req(NV_SECTION_UI_PHONEBOOK, 0, length, (uint8_t *)nvm_clear)) {
        printf("clear nvm error in phonebook_write_nvm_for_test\n");
    }
    lv_mem_free(nvm_clear);

    length = sizeof(nvm_list);
    if(length != UI_NV_Write_Req(NV_SECTION_UI_PHONEBOOK, 0, length, (uint8_t *)(&nvm_list))) {
        printf("write nvm error in phonebook_write_nvm_for_test\n");
    }

    _lv_ll_init(&test_msgs_list, sizeof(app_adaptor_voice_msg_info_t));
    test_msgs_count = 0;
    test_voice_msg_count = 0;
#if VOICE_MSG_APP_SPECIFIC_TEST != 0
    voice_msg_test_rcv_msg_task = NULL;
#endif

    uint32_t buf_size;
    void * buf = voice_msg_read_file_data(AUDIO_POWER_ON, VOICE_MSG_TYPE_VOICE, &buf_size);
    voice_msg_test_write_file(buf, buf_size, (char *)VOICE_MSG_TEST_AUDIO_POWER_ON);
    lv_mem_free(buf);
    buf = voice_msg_read_file_data(AUDIO_POWER_OFF, VOICE_MSG_TYPE_VOICE, &buf_size);
    voice_msg_test_write_file(buf, buf_size, (char *)VOICE_MSG_TEST_AUDIO_POWER_OFF);
    lv_mem_free(buf);

    app_adaptor_voice_msg_info_t * msg = _lv_ll_ins_tail(&test_msgs_list);
    Hal_Mem_Set(msg, 0, sizeof(app_adaptor_voice_msg_info_t));
    msg->direction = WATCH_VOICE_MSG_FROM_OTHER_DEVICE;
    msg->read_flag = false;
    msg->time.year = 2017;
    msg->time.month = 11;
    msg->time.day = 29;
    msg->time.hour = 2;
    msg->time.min = 6;
    msg->type = WATCH_VOICE_MSG_TYPE_TEXT;
    char text[] = {"Hello!"};
    msg->data_type = WATCH_VOICE_DATA_TYPE_FILE;
    msg->content.text = voice_msg_test_set_file_path(1, WATCH_VOICE_MSG_TYPE_TEXT);
    voice_msg_test_write_file(text, strlen(text), msg->content.text);
    test_msgs_count++;

    msg = _lv_ll_ins_tail(&test_msgs_list);
    Hal_Mem_Set(msg, 0, sizeof(app_adaptor_voice_msg_info_t));
    msg->direction = WATCH_VOICE_MSG_FROM_OTHER_DEVICE;
    msg->read_flag = false;
    msg->time.year = 2017;
    msg->time.month = 11;
    msg->time.day = 30;
    msg->time.hour = 8;
    msg->time.min = 56;
    msg->type = WATCH_VOICE_MSG_TYPE_VOICE;
    msg->data_type = WATCH_VOICE_DATA_TYPE_FILE;
    msg->content.voice.voice_len = strlen(VOICE_MSG_TEST_AUDIO_POWER_ON);
    msg->content.voice.file = (uint8_t *)Hal_Mem_Alloc(msg->content.voice.voice_len + 1);
    Hal_Mem_Copy(msg->content.voice.file,
                 VOICE_MSG_TEST_AUDIO_POWER_ON,
                 msg->content.voice.voice_len + 1);
    msg->content.voice.index = test_voice_msg_count++;
    test_msgs_count++;

    msg = _lv_ll_ins_tail(&test_msgs_list);
    Hal_Mem_Set(msg, 0, sizeof(app_adaptor_voice_msg_info_t));
    msg->direction = WATCH_VOICE_MSG_FROM_OTHER_DEVICE;
    msg->read_flag = false;
    msg->time.year = 2017;
    msg->time.month = 11;
    msg->time.day = 30;
    msg->time.hour = 8;
    msg->time.min = 58;
    msg->type = WATCH_VOICE_MSG_TYPE_VOICE;
    msg->data_type = WATCH_VOICE_DATA_TYPE_FILE;
    msg->content.voice.voice_len = strlen(VOICE_MSG_TEST_AUDIO_POWER_OFF);
    msg->content.voice.file = (uint8_t *)Hal_Mem_Alloc(msg->content.voice.voice_len + 1);
    Hal_Mem_Copy(msg->content.voice.file,
                 VOICE_MSG_TEST_AUDIO_POWER_OFF,
                 msg->content.voice.voice_len + 1);
    msg->content.voice.index = test_voice_msg_count++;
    test_msgs_count++;

    msg = _lv_ll_ins_tail(&test_msgs_list);
    Hal_Mem_Set(msg, 0, sizeof(app_adaptor_voice_msg_info_t));
    hal_rtc_t cur_time;
    Hal_Rtc_Gettime(&cur_time);
    msg->direction = WATCH_VOICE_MSG_FROM_UI;
    msg->read_flag = false;
    hal_rtc_t tm;
    if((1970 == cur_time.tm_year)
        && (1 == cur_time.tm_mon)
       && (1 == cur_time.tm_mday)) {
        memcpy(&tm, &cur_time, sizeof(hal_rtc_t));
    } else {
        seconds_to_time(time_to_seconds(&cur_time) - 23 * 3600, &tm);
    }
    msg->time.year = tm.tm_year;
    msg->time.month = tm.tm_mon;
    msg->time.day = tm.tm_mday;
    msg->time.hour = tm.tm_hour;
    msg->time.min = tm.tm_min;
    msg->type = WATCH_VOICE_MSG_TYPE_VOICE;
    msg->data_type = WATCH_VOICE_DATA_TYPE_FILE;
    msg->content.voice.voice_len = strlen(VOICE_MSG_TEST_AUDIO_POWER_ON);
    msg->content.voice.file = (uint8_t *)Hal_Mem_Alloc(msg->content.voice.voice_len + 1);
    Hal_Mem_Copy(msg->content.voice.file,
                 VOICE_MSG_TEST_AUDIO_POWER_ON,
                 msg->content.voice.voice_len + 1);
    msg->content.voice.index = test_voice_msg_count++;
    test_msgs_count++;

    msg = _lv_ll_ins_tail(&test_msgs_list);
    Hal_Mem_Set(msg, 0, sizeof(app_adaptor_voice_msg_info_t));
    msg->direction = WATCH_VOICE_MSG_FROM_OTHER_DEVICE;
    msg->read_flag = false;
    msg->time.year = cur_time.tm_year;
    msg->time.month = cur_time.tm_mon;
    msg->time.day = cur_time.tm_mday;
    msg->time.hour = cur_time.tm_hour;
    msg->time.min = cur_time.tm_min;
    msg->type = WATCH_VOICE_MSG_TYPE_EXPRESSION;
    msg->data_type = WATCH_VOICE_DATA_TYPE_FILE;
    msg->content.img.data_size = strlen(ICON_EXPRESSION_TEST);
    msg->content.img.data = (uint8_t *)Hal_Mem_Alloc(msg->content.img.data_size + 1);
    Hal_Mem_Copy(msg->content.img.data, ICON_EXPRESSION_TEST, msg->content.img.data_size + 1);
    test_msgs_count++;
}

#ifdef BUILD_IN_PC_SIMULATOR /*for pc test*/
void voice_msg_test_record_buffer_start_req(uint8_t * buffer, uint32_t bufsize, uint32_t maxDurationMs, AUDIO_RECORD_CB_FUNC func)
{
    max_duration = maxDurationMs / 1000;
    printf("%s: maxduration %d\n", __FUNCTION__, max_duration);
    func_record_test = func;
    duration_test = 0;
    voice_msg_test_task_record = lv_task_create(voice_msg_test_task_record_cb,
                                   1000, LV_TASK_PRIO_MID, NULL);
}

uint32_t voice_msg_test_record_buffer_stop_req(uint32_t * recSize, uint32_t * durationMs)
{
    *recSize = VOICE_MSG_AMR_HEAD_SIZE + duration_test * HAL_AMR_BUFSIZE_PER_SECOND;
    printf("%s: size %d\n", __FUNCTION__, *recSize);
    if(voice_msg_test_task_record) {
        lv_task_del(voice_msg_test_task_record);
        voice_msg_test_task_record = NULL;
    }

    return 0;
}

void voice_msg_test_file_play_end(void)
{
    printf("%s\n", __FUNCTION__);
    lv_task_del(voice_msg_test_task_record);
    voice_msg_test_task_record = NULL;
}

void voice_msg_test_tone_play_end(void)
{
    printf("%s\n", __FUNCTION__);
    lv_task_del(voice_msg_test_task_record);
    voice_msg_test_task_record = NULL;
}

void voice_msg_test_file_play_onetime(const char *path, HAL_SPEAKER_GAIN volume, CB_FUNC func, void *para)
{
    func_voice_test = func;
    uint32_t time = 5000;
    printf("%s: time %d\n", __FUNCTION__, time);
    voice_msg_test_task_record = lv_task_create(voice_msg_test_task_voice_cb, time, LV_TASK_PRIO_MID, para);
    lv_task_once(voice_msg_test_task_record);
}

void voice_msg_test_tone_play_onetime(uint32_t *buffer, uint32_t len, HAL_SPEAKER_GAIN volume, CB_FUNC func,
                             void *para)
{
    func_voice_test = func;
    uint32_t time = len / HAL_AMR_BUFSIZE_PER_SECOND * 1000;
    printf("%s: time %d\n", __FUNCTION__, time);
    voice_msg_test_task_record = lv_task_create(voice_msg_test_task_voice_cb, time, LV_TASK_PRIO_MID, para);
    lv_task_once(voice_msg_test_task_record);
}
#endif

#if VOICE_MSG_APP_SPECIFIC_TEST == 0
void voice_msg_test_clear_nvm(void)
{
    printf("%s\n", __FUNCTION__);
    uint32_t len = sizeof(nv_watch_phonebook_t);
    nv_watch_phonebook_t * nvm_pb = (nv_watch_phonebook_t *)lv_mem_alloc(len);
    memset(nvm_pb, 0, len);
    len = UI_NV_Write_Req(NV_SECTION_UI_PHONEBOOK, 0, len, (uint8_t *)nvm_pb);
    lv_mem_free(nvm_pb);

    len = sizeof(nv_watch_voice_msg_t);
    nv_watch_voice_msg_t * nvm = (nv_watch_voice_msg_t *)lv_mem_alloc(len);
    memset(nvm, 0xFF, len);
    UI_NV_Write_Req(NV_SECTION_UI_VOICE_MSG, 0, len, (uint8_t *)nvm);
    lv_mem_free(nvm);
}

void voice_msg_test_clear(void)
{
    app_adaptor_voice_msg_info_t * msg = _lv_ll_get_head(&test_msgs_list);
    while(msg) {
        voice_msg_test_delete_info(msg);
        msg = _lv_ll_get_next(&test_msgs_list, msg);
    }
    _lv_ll_clear(&test_msgs_list);

    if(test_name) {
        lv_mem_free(test_name);
        test_name = NULL;
    }
}
#endif

app_adaptor_voice_msg_t * app_adaptor_voice_msg_get_msgs_req(app_adaptor_voice_msg_chat_id_t * id)
{
    app_adaptor_voice_msg_t * msgs = (app_adaptor_voice_msg_t *)lv_mem_alloc(sizeof(app_adaptor_voice_msg_t));
    memset(msgs, 0, sizeof(app_adaptor_voice_msg_t));
    msgs->chat_type = id->chat_type;
    msgs->name = id->name;
    msgs->number = id->number;
    lv_mem_free(id);
    if(test_name) Hal_Mem_Free(test_name);
    test_name = (char *)lv_mem_alloc(strlen(msgs->name) + 1);
    memcpy(test_name, msgs->name, strlen(msgs->name) + 1);
    msgs->count = test_msgs_count;
    if(0 < msgs->count) {
        uint32_t len = sizeof(app_adaptor_voice_msg_info_t) * msgs->count;
        msgs->msg = (app_adaptor_voice_msg_info_t *)Hal_Mem_Alloc(len);
        memset(msgs->msg, 0, len);
        uint8_t name_len;
        char * name_str;
        app_adaptor_voice_msg_info_t * test_msg = _lv_ll_get_head(&test_msgs_list);
        uint8_t i = 0;
        while(test_msg) {
            msgs->msg[i].direction = test_msg->direction;
            msgs->msg[i].read_flag = test_msg->read_flag;
            msgs->msg[i].type = test_msg->type;
            msgs->msg[i].data_type = test_msg->data_type;
            memcpy(&msgs->msg[i].time, &test_msg->time, sizeof(app_adp_time_and_date_t));
            if(true == voice_msg_test_is_group_msg(msgs->chat_type, msgs->name)) {
                if(i % 3) {
                    name_str = "admin";
                } else {
                    name_str = "other 1";
                }
                name_len = strlen(name_str) + 1;
                msgs->msg[i].name = (char *)lv_mem_alloc(name_len);
                memcpy(msgs->msg[i].name, name_str, name_len);
            }
            if(WATCH_VOICE_MSG_TYPE_TEXT == test_msg->type) {
                len = strlen(test_msg->content.text) + 1;
                msgs->msg[i].content.text = (char *)lv_mem_alloc(len);
                memcpy(msgs->msg[i].content.text, test_msg->content.text, len);
            } else if(WATCH_VOICE_MSG_TYPE_VOICE == test_msg->type) {
                msgs->msg[i].content.voice.voice_len = test_msg->content.voice.voice_len;
                if(WATCH_VOICE_DATA_TYPE_FILE == msgs->msg[i].data_type) {
                    uint32_t size = strlen((char *)test_msg->content.voice.file) + 1;
                    msgs->msg[i].content.voice.file = (uint8_t *)lv_mem_alloc(size);
                    strncpy((char *)msgs->msg[i].content.voice.file,
                            (char *)test_msg->content.voice.file,
                            size);
                } else {
                    msgs->msg[i].content.voice.file
                        = (uint8_t *)lv_mem_alloc(msgs->msg[i].content.voice.voice_len);
                    memcpy(msgs->msg[i].content.voice.file,
                           test_msg->content.voice.file,
                           msgs->msg[i].content.voice.voice_len);
                }
                msgs->msg[i].content.voice.index = test_msg->content.voice.index;
            } else if((WATCH_VOICE_MSG_TYPE_EXPRESSION == test_msg->type)
                      || WATCH_VOICE_MSG_TYPE_PHOTO == test_msg->type) {
                msgs->msg[i].content.img.data_size = test_msg->content.img.data_size;
                if(WATCH_VOICE_DATA_TYPE_FILE == msgs->msg[i].data_type) {
                    uint32_t size = strlen((char *)test_msg->content.img.data) + 1;
                    msgs->msg[i].content.img.data = (uint8_t *)lv_mem_alloc(size);
                    strncpy((char *)msgs->msg[i].content.img.data,
                            (char *)test_msg->content.img.data,
                            size);
                } else {
                    msgs->msg[i].content.img.data
                        = (uint8_t *)lv_mem_alloc(msgs->msg[i].content.img.data_size);
                    memcpy(msgs->msg[i].content.img.data,
                           test_msg->content.img.data,
                           msgs->msg[i].content.img.data_size);
                }
            }
            i++;
            test_msg = _lv_ll_get_next(&test_msgs_list, test_msg);
        }
    }

#if VOICE_MSG_APP_SPECIFIC_TEST != 0
    if(NULL == voice_msg_test_rcv_msg_task) {
        printf("%s: rcv a msg after %ds for test\n",
               __FUNCTION__, VOICE_MSG_TEST_RCV_MSG_PERIOD / 1000);
        voice_msg_test_rcv_msg_task = lv_task_create(voice_msg_test_rcv_msg_task_cb,
                                                     VOICE_MSG_TEST_RCV_MSG_PERIOD,
                                                     LV_TASK_PRIO_MID, NULL);
        lv_task_set_repeat_count(voice_msg_test_rcv_msg_task, VOICE_MSG_TEST_RCV_MSG_NUM);
    }
#endif

    return msgs;
}

char * app_adaptor_voice_msg_send_req(app_adaptor_voice_msg_t * msg)
{
    printf("%s: name %s, test_msgs_count %d\n",
           __FUNCTION__, msg->name, test_msgs_count);

    lv_task_t * send_test_task = lv_task_create(voice_msg_adaptor_send_cnf,
                                                1500,
                                                LV_TASK_PRIO_HIGH,
                                                NULL);
    lv_task_once(send_test_task);

    app_adaptor_voice_msg_info_t * test_msg = _lv_ll_ins_tail(&test_msgs_list);
    memcpy(test_msg, msg->msg, sizeof(app_adaptor_voice_msg_info_t));
    if(WATCH_VOICE_MSG_TYPE_VOICE == msg->msg->type) {
        test_msg->content.voice.index = test_voice_msg_count++;
    }
    test_msgs_count++;

    if(msg->name) lv_mem_free(msg->name);
    if(msg->number) lv_mem_free(msg->number);
    lv_mem_free(msg->msg);
    lv_mem_free(msg);

    return NULL;
}

void app_adaptor_voice_msg_read_voice_req(app_adaptor_voice_msg_chat_id_t * id, uint8_t index)
{
    printf("%s: read voice, chat type %d, name %s, index %d\n",
           __FUNCTION__, id->chat_type, id->name, index);

    app_adaptor_voice_msg_info_t * test_msg = _lv_ll_get_head(&test_msgs_list);
    while(test_msg) {
        if((WATCH_VOICE_MSG_TYPE_VOICE == test_msg->type)
            && (index == test_msg->content.voice.index)) {
            if(true == test_msg->read_flag) {
                printf("%s: read flag error\n", __FUNCTION__);
            } else {
                test_msg->read_flag = true;
            }
            break;
        }
        test_msg = _lv_ll_get_next(&test_msgs_list, test_msg);
    }

    if(id->name) lv_mem_free(id->name);
    if(id->number) lv_mem_free(id->number);
    lv_mem_free(id);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static bool voice_msg_test_is_group_msg(watch_app_adp_voice_msg_chat_type_t chat_type, char * name)
{
    bool group_msg = false;
    if((WATCH_VOICE_MSG_FAMILY_GROUP_CHAT == chat_type)
            || (WATCH_VOICE_MSG_FRIENDS_GROUP_CHAT == chat_type)) {
        group_msg = true;
    } else if((WATCH_VOICE_MSG_INVALID_CHAT == chat_type) && (NULL != name)) {
        const char * family_group = lv_lang_get_text(WATCH_TEXT_ID_FAMILY_GROUP);
        const char * friends_group = lv_lang_get_text(WATCH_TEXT_ID_FRIENDS_GROUP);
        if((0 == strcmp(name, family_group))
                || (0 == strcmp(name, friends_group))) {
            group_msg = true;
        }

    }
    printf("%s: group msg %d\n", __FUNCTION__, group_msg);

    return group_msg;
}

static void voice_msg_adaptor_send_cnf(lv_task_t * task)
{
    printf("%s\n", __FUNCTION__);
#if 0
    lv_voice_msg_chat_ext_t * ext = voice_msg_get_ext(ACT_ID_VOICE_MSG_CHAT);
    if(NULL == ext) return;
    if(ext->cur_contact) {
        app_adaptor_voice_msg_send_cnf(WATCH_VOICE_MSG_RESULT_SUCCESS);
    } else {
        app_adaptor_voice_msg_send_cnf(WATCH_VOICE_MSG_RESULT_FAILURE);
    }
#else
    app_adaptor_voice_msg_send_cnf(WATCH_VOICE_MSG_RESULT_SUCCESS);
#endif
}

#ifdef BUILD_IN_PC_SIMULATOR /*for pc test*/
static void voice_msg_test_task_record_cb(lv_task_t * task)
{
    printf("%s\n", __FUNCTION__);
    duration_test++;
    if(max_duration == duration_test) {
        func_record_test(MCI_EVENT_EOS, MCI_INFO_RECORDER_CUR_DURATION_MS, 0);
        lv_task_del(task);
        voice_msg_test_task_record = NULL;
    } else {
        func_record_test(MCI_EVENT_INFO, MCI_INFO_RECORDER_CUR_DURATION_MS, duration_test * 1000);
    }
}

static void voice_msg_test_task_voice_cb(lv_task_t * task)
{
    printf("%s\n", __FUNCTION__);
    voice_msg_test_task_record = NULL;
    if(func_voice_test) {
        func_voice_test(task->user_data);
    }
}
#endif

static char * voice_msg_test_set_file_path(uint8_t index, uint8_t file_type)
{
    const char * name;
    if(WATCH_VOICE_MSG_TYPE_TEXT == file_type) {
        name = VOICE_MSG_TEST_FILE_TEXT;
    } else if(WATCH_VOICE_MSG_TYPE_VOICE == file_type) {
        name = VOICE_MSG_TEST_FILE_VOICE;
    } else {
        return NULL;
    }
    uint8_t len = strlen(VOICE_MSG_TEST_FILE_DIR) + 2 + 1;
    len += strlen(name);
    char * path = (char *)lv_mem_alloc(len);
    snprintf(path, len, "%s%02d%s", VOICE_MSG_TEST_FILE_DIR, index, name);

    return path;
}

static void voice_msg_test_write_file(void * data, uint32_t size, char * file_path)
{
    printf("%s: file path %s\n", __FUNCTION__, file_path);

    lv_fs_file_t file = {};
    lv_fs_open(&file, (const char *)file_path, LV_FS_MODE_WR);

    if(NULL == file.file_d) {
        printf("%s: please check file path\n", __FUNCTION__);
        return;
    }

    lv_fs_seek(&file, SEEK_SET);
    uint32_t write_size;
    lv_fs_write(&file, data, size, &write_size);
    if(0 == write_size) {
        printf("%s: write in fs is wrong\n", __FUNCTION__);
        return;
    }
    lv_fs_close(&file);

    return;
}

#if VOICE_MSG_APP_SPECIFIC_TEST != 0
static void voice_msg_test_fill_id(app_adaptor_voice_msg_t * msg)
{
    msg->name = NULL;
    msg->number = NULL;
    if(0 == strcmp(test_name, lv_lang_get_text(WATCH_TEXT_ID_FAMILY_GROUP))) {
        msg->chat_type = WATCH_VOICE_MSG_FAMILY_GROUP_CHAT;
        char contact[] = {"admin"};
        msg->msg->name = (char *)lv_mem_alloc(strlen(contact) + 1);
        memcpy(msg->msg->name, contact, strlen(contact) + 1);
    } else if(0 == strcmp(test_name, lv_lang_get_text(WATCH_TEXT_ID_FRIENDS_GROUP))) {
        msg->chat_type = WATCH_VOICE_MSG_FRIENDS_GROUP_CHAT;
        char contact[] = {"admin"};
        msg->msg->name = (char *)lv_mem_alloc(strlen(contact) + 1);
        memcpy(msg->msg->name, contact, strlen(contact) + 1);
    } else {
        msg->chat_type = WATCH_VOICE_MSG_INVALID_CHAT;
        msg->name = (char *)lv_mem_alloc(strlen(test_name) + 1);
        memcpy(msg->name, test_name, strlen(test_name) + 1);
    }
}

static void voice_msg_test_rcv_msg_task_cb(lv_task_t * task)
{
    printf("%s: repeat_count %d, test_msgs_count %d\n",
           __FUNCTION__, task->repeat_count, test_msgs_count);

    app_adaptor_voice_msg_info_t * test_msg = _lv_ll_ins_tail(&test_msgs_list);
    memset(test_msg, 0, sizeof(app_adaptor_voice_msg_info_t));
    hal_rtc_t cur_time;
    Hal_Rtc_Gettime(&cur_time);
    test_msg->direction = WATCH_VOICE_MSG_FROM_OTHER_DEVICE;
    test_msg->read_flag = false;
    test_msg->time.year = cur_time.tm_year;
    test_msg->time.month = cur_time.tm_mon;
    test_msg->time.day = cur_time.tm_mday;
    test_msg->time.hour = cur_time.tm_hour;
    test_msg->time.min = cur_time.tm_min;

    /*send new message to ui*/
    app_adaptor_voice_msg_t * msg = (app_adaptor_voice_msg_t *)lv_mem_alloc(sizeof(app_adaptor_voice_msg_t));
    memset(msg, 0, sizeof(app_adaptor_voice_msg_t));
    msg->count = 1;
    msg->msg = (app_adaptor_voice_msg_info_t *)lv_mem_alloc(sizeof(app_adaptor_voice_msg_info_t));
    memset(msg->msg, 0, sizeof(app_adaptor_voice_msg_info_t));
    voice_msg_test_fill_id(msg);
    if((WATCH_VOICE_MSG_FAMILY_GROUP_CHAT == msg->chat_type) || (WATCH_VOICE_MSG_FRIENDS_GROUP_CHAT == msg->chat_type)) {
        char contact[] = {"admin"};
        msg->msg->name = (char *)lv_mem_alloc(strlen(contact) + 1);
        memcpy(msg->msg->name, contact, strlen(contact) + 1);
    }
    msg->msg->direction = test_msg->direction;
    msg->msg->read_flag = test_msg->read_flag;
    memcpy(&msg->msg->time, &test_msg->time, sizeof(app_adp_time_and_date_t));

    if(VOICE_MSG_TEST_RCV_MSG_NUM == task->repeat_count) {
        test_msg->type = WATCH_VOICE_MSG_TYPE_VOICE;
        test_msg->data_type = WATCH_VOICE_DATA_TYPE_FILE;
        test_msg->content.voice.index = test_voice_msg_count++;
        test_msg->content.voice.voice_len = strlen(AUDIO_POWER_OFF);
        test_msg->content.voice.file
            = (uint8_t *)lv_mem_alloc(test_msg->content.voice.voice_len + 1);
        memcpy(test_msg->content.voice.file, AUDIO_POWER_OFF, test_msg->content.voice.voice_len + 1);

        msg->msg->type = test_msg->type;
        msg->msg->data_type = test_msg->data_type;
        msg->msg->content.voice.index = test_msg->content.voice.index;
        msg->msg->content.voice.voice_len = test_msg->content.voice.voice_len;
        msg->msg->content.voice.file
            = (uint8_t *)lv_mem_alloc(msg->msg->content.voice.voice_len + 1);
        memcpy(msg->msg->content.voice.file,
               test_msg->content.voice.file,
               msg->msg->content.voice.voice_len + 1);
    } else if((VOICE_MSG_TEST_RCV_MSG_NUM - 1) == task->repeat_count) {
        test_msg->type = WATCH_VOICE_MSG_TYPE_TEXT;
        test_msg->data_type = WATCH_VOICE_DATA_TYPE_BUFFER;
        char text[] = {"Welcome to voice msg"};
        uint8_t len = strlen(text) + 1;
        test_msg->content.text = (char *)lv_mem_alloc(len);
        memcpy(test_msg->content.text, text, len);

        msg->msg->type = test_msg->type;
        msg->msg->data_type = test_msg->data_type;
        msg->msg->content.text = (char *)lv_mem_alloc(len);
        memcpy(msg->msg->content.text, text, len);
    } else if(1 == task->repeat_count) {
        voice_msg_test_rcv_msg_task = NULL; // task will be deleted by lvgl

        test_msg->type = WATCH_VOICE_MSG_TYPE_EXPRESSION;
        test_msg->data_type = WATCH_VOICE_DATA_TYPE_FILE;
        test_msg->content.img.data_size = strlen(ICON_EXPRESSION_TEST);
        test_msg->content.img.data = (uint8_t *)lv_mem_alloc(test_msg->content.img.data_size + 1);
        memcpy(test_msg->content.img.data,
               ICON_EXPRESSION_TEST,
               test_msg->content.img.data_size + 1);

        msg->msg->type = test_msg->type;
        msg->msg->data_type = test_msg->data_type;
        msg->msg->content.img.data_size = test_msg->content.img.data_size;
        msg->msg->content.img.data = (uint8_t *)lv_mem_alloc(msg->msg->content.img.data_size + 1);
        memcpy(msg->msg->content.img.data, ICON_EXPRESSION_TEST, msg->msg->content.img.data_size + 1);
    } else {
        lv_mem_free(msg);
        _lv_ll_remove(&test_msgs_list, test_msg);
        lv_mem_free(test_msg);
        return;
    }
    test_msgs_count++;
    app_adaptor_voice_msg_rcv_ind(msg);
}
#else
static void voice_msg_test_delete_info(app_adaptor_voice_msg_info_t * msg)
{
    if(WATCH_VOICE_MSG_TYPE_TEXT == msg->type) {
        if(WATCH_VOICE_DATA_TYPE_FILE == msg->data_type) {
            /*delete file*/
            printf("%s: file name %s\n", __FUNCTION__, msg->content.text);
            lv_fs_remove(msg->content.text);
        }
        Hal_Mem_Free(msg->content.text);
    } else if(WATCH_VOICE_MSG_TYPE_VOICE == msg->type) {
        Hal_Mem_Free(msg->content.voice.file);
    } else if((WATCH_VOICE_MSG_TYPE_EXPRESSION == msg->type)
              || WATCH_VOICE_MSG_TYPE_PHOTO == msg->type) {
        Hal_Mem_Free(msg->content.img.data);
    } else {
        printf("%s: wrong msg type %d\n", __FUNCTION__, msg->type);
    }
}
#endif

#endif /*VOICE_MSG_TEST*/

#endif /*USE_LV_WATCH_VOICE_MSG*/
